﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dryricenet
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public string jian = ""; //几件商品

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["Name"] == null)
            {
                //登录状态为空
                Response.Write("<script>javascript:alert('对不起,您还没有登录!');history.back();</script>");
                Response.End();
            }
            else if (!IsPostBack)
            {
                string name = Session["Name"].ToString();
                DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                int id = Convert.ToInt32(dr["UserID"]);
                Literal1.Text = CartBLL.totalBLL(id).ToString();
                CK(id);
                jian = CartBLL.jjspBLL(id).ToString();
            }

        }
        //购物车查看
        public void CK(int id)
        {
            Repeater1.DataSource = CartBLL.cartCKBll(id);
            Repeater1.DataBind();
        }

        protected void Delete_Click(object sender, EventArgs e)
        {

        }
        //删除
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            int id = Convert.ToInt32(dr["UserID"]);
            int ids = 0;
            if (e.CommandName == "Delete")
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "", true);
                ids = Convert.ToInt32(e.CommandArgument);//删除处理
            }
            foodBLL.cartKCSLBLL(ids);
            CartBLL.delIDBLL(ids);
                CK(id);
            
        }
            //提交订单
            protected void txtOrder_Click(object sender, EventArgs e)
            {
                string name = Session["Name"].ToString();
                DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                int id = Convert.ToInt32(dr["UserID"]);
                if (CartBLL.cartCKBll(id).Rows.Count != 0)
                {
                    Response.Redirect("home_Order.aspx");
                }
                else
                {
                    Response.Write("<Script Language='JavaScript'>window.alert('" + "购物车里还没有美食，赶紧去订餐吧！" + "');</script>");
                }
            }
        }
    }